#!/bin/bash

# upzooomr -u username -p password [-t <tag>] [-t <tag2>] [-n] [-r] [-a] file1 file2 ...
# -u: username (e-mail)
# -p: password
# -t: Tags
# -p: private
# -r: friend
# -a: family

cookiejar=/tmp/cookie.$$
username=""
password=""
tags=""
family=0
friend=0
public=1

while getopts "u:p:t:nra" name
do
	case "$name" in
		u) username="$OPTARG"
			;;
		p) password="$OPTARG"
			;;
		a) family=1
			;;
		r) friend=1
			;;
		n) public=0
			;;
		t) tags="$OPTARG $tags"
			;;
		?) exit 1
			;;
	esac
done
(( SHIFT = OPTIND - 2 ))
shift $SHIFT

echo Uploading with following Tags: $tags
echo Public: $public | sed 's/0/no/;s/1/yes/'
echo Friend: $friend | sed 's/0/no/;s/1/yes/'
echo Family: $family | sed 's/0/no/;s/1/yes/'

echo -n "Logging in ... "
if curl -c $cookiejar -d "username=$username&password=$password&processlogin=1&gogogo=1" http://de.zooomr.com/login/
then
	echo ok
else
	echo NOT OK
	rm $cookiejar
	exit 2
fi

while shift
do
	if [[ "x$1" == "x" ]]
	then
		continue
	fi
	if [[ -f "$1" ]]
	then
		echo -n "Uploading $1 ... "
		if curl -H "Expect:" -b $cookiejar -F "done=1" -F "labels=$tags" -F "is_public=$public" -F "is_friend=$friend" -F "is_family=$family" -F "Filedata=@$1" http://upload.zooomr.com/photos/upload/process/
		then
			echo ok
		else
			echo NOT OK
		fi
	else
		echo "File $1 doesn't exist, not uploading."
	fi
done

echo -n "Logging out ... "
if curl -b $cookiejar -s http://de.zooomr.com/logout/ >/dev/null
then
	echo ok
else
	echo NOT OK
	rm $cookiejar
	exit 3
fi
rm $cookiejar
